package leetcode.other.p128;

import java.util.Arrays;

class Solution {
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        int maxLen = 1;
        int j = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] - nums[i - 1] == 1) {
                j++;
            } else if(nums[i] - nums[i - 1] > 1) {
                maxLen = Math.max(maxLen, j);
                j = 1;
            }
        }
        return j > 1 ? Math.max(j, maxLen) : maxLen;
    }

    public static void main(String[] args) {
        // 示例 1：
        // 输入：nums = [100,4,200,1,3,2]
        // 输出：4
        /*System.out.println(new Solution().longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
        // 解释：最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
        // 示例 2：
        // 输入：nums = [0,3,7,2,5,8,4,6,0,1]
        // 输出：9
        System.out.println(new Solution().longestConsecutive(new int[]{0, 3, 7, 2, 5, 8, 4, 6, 0, 1}));
        // 示例 3：
        // 输入：nums = [1,0,1,2]
        // 输出：3
        System.out.println(new Solution().longestConsecutive(new int[]{1, 0, 1, 2}));*/
        System.out.println(new Solution().longestConsecutive(new int[]{9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6}));
    }
}